package com.capt_4;

import com.capt_4.equation.AddEquation;
import com.capt_4.equation.Equation;
import com.capt_4.equation.SubEquation;

import java.util.ArrayList;
import java.util.Random;

public class Exercise {
    private ArrayList<Equation> list = null;
    private int exerciseNumber;

    public ArrayList<Equation> getList() {
        return list;
    }

    public void setList(ArrayList<Equation> list) {
        this.list = list;
    }

    public int getExerciseNumber() {
        return exerciseNumber;
    }

    public void setExerciseNumber(int exerciseNumber) {
        this.exerciseNumber = exerciseNumber;
    }

    public Exercise(){
        list = new ArrayList<>();
        this.exerciseNumber = exerciseNumber;
    }

//    打印习题集合
    public void printExercise(){
        int i = 0;
        for (Equation equation : list) {
            i++;
            Equation a = equation;
            System.out.print("第"+i+"题");
            System.out.print(a.toString());
            System.out.println();

            if (i%5 == 0){
                System.out.println();
            }
        }

    }

//    判断习题集是否相同
    public boolean isContain(Equation e){
        boolean flag = false;
        for (int i = 0; i < list.size(); i++) {
            if(e.isEqual(list.get(i))){
                flag = true;
                break;
            }
        }
        return flag;
    }

    //  产生加法习题集
    /**
     * 循环50次,产生加法算式,
     *          判断是否重复
     *          如果不重复,添加到算式集合
     *          否则重新产生
     */

    public void generateAddExercise(){
        for (int i = 0; i < exerciseNumber; ) {
            AddEquation e = new AddEquation();
            e.generateEquattion('+');
            // 判断是否重复
            if (isContain(e)){
                continue;
            } else {
                list.add(e);
                i++;
            }
        }
    }

    public void generateSubExercise(){
        for (int i = 0; i < exerciseNumber; ) {
            SubEquation e = new SubEquation();
            e.generateEquattion('-');
            // 判断是否重复
            if (isContain(e)){
                continue;
            } else {
                list.add(e);
                i++;
            }
        }
    }


    public void generateRandomExercise(){
        for (int i = 0; i < exerciseNumber; ) {
//            System.out.println("循环："+i);
            Random random = new Random();
            Equation e = null;

            if (random.nextInt(2) == 0) {
//                System.out.println("生成加法：");
                e = new AddEquation();
                e.generateEquattion('+');
            } else {
//                System.out.println("生成减法：");
              e = new SubEquation();
                e.generateEquattion('-');
            }
            // 判断是否重复
            if (isContain(e)){
                continue;
            } else {
                list.add(e);
                i++;
            }
        }
    }
}



